package com.jinmdz.fmis.dao.admin.dao;

import com.jinmdz.fmis.dao.admin.model.dictionarydata.DictionaryDataEditItem;
import com.jinmdz.fmis.dao.admin.model.dictionarydata.DictionaryDataListItem;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

import java.util.ArrayList;

/**
 * 字典Dao
 *
 * @author GuanKui
 * @date 2020/3/9 15:13
 */
public interface DictionaryDataDao {

    /**
     * 根据dict_code获取字典信息
     *
     * @param dictCode 查询编码
     * @return
     * @author GuanKui
     * @date 2020/3/9  15:35
     */
    @Select(" select id,dict_code,data_code,data_text,data_text_abbr,data_value,data_describe,as_default,disable_state,sort,remark,unmodified,version " +
            " from data_dictionary where deleted=0 and dict_code = #{dictCode} order by sort ")
    ArrayList<DictionaryDataListItem> listDictionaryDataListByCode(@Param("dictCode") String dictCode);


    /**
     * 根据id获取字典信息
     *
     * @param id 表ID
     * @return
     * @author GuanKui
     * @date 2020/3/9  15:35
     */
    @Select(" select id,dict_code,data_code,data_text,data_text_abbr,data_value,data_describe,as_default,disable_state,sort,remark,unmodified,version " +
            " from data_dictionary where id = #{id} and deleted = 0 limit 1 ")
    DictionaryDataEditItem getDictionaryDataEditById(@Param("id") Integer id);

    /**
     * 按照字典数据编码查询非当前字典主键
     * 删除也不可重复
     *
     * @param dictionaryId 当前字典主键
     * @param dictCode     表中字典编码
     * @param dataCode     字典数据编码
     * @return
     * @author LiCongLu
     * @date 2020-03-10 11:50
     */
    @Select(" select id from data_dictionary where id != IFNULL(#{dictionaryId},0) and dict_code = #{dictCode} and data_code = #{dataCode} limit 1 ")
    @ResultType(Integer.class)
    Integer getDictionaryIdByDataCode(@Param("dictionaryId") Integer dictionaryId, @Param("dictCode") String dictCode, @Param("dataCode") Integer dataCode);

    /**
     * 按照字典数据编码文本值查询非当前字典主键
     *
     * @param dictionaryId 当前字典主键
     * @param dictCode     表中字典编码
     * @param dataText     字典数据文本值
     * @return
     * @author LiCongLu
     * @date 2020-03-10 11:50
     */
    @Select(" select id from data_dictionary where id != IFNULL(#{dictionaryId},0) and dict_code = #{dictCode} and data_text = #{dataText}  and deleted = 0 limit 1 ")
    @ResultType(Integer.class)
    Integer getDictionaryIdByDataText(@Param("dictionaryId") Integer dictionaryId, @Param("dictCode") String dictCode, @Param("dataText") String dataText);

    /**
     * 按照默认值查询非当前字典主键
     *
     * @param dictionaryId 当前字典主键
     * @param dictCode     表中字典编码
     * @return
     * @author LiCongLu
     * @date 2020-03-10 11:50
     */
    @Select(" select id from data_dictionary where id != IFNULL(#{dictionaryId},0) and dict_code = #{dictCode} and as_default = 1 and deleted = 0 limit 1 ")
    @ResultType(Integer.class)
    Integer getDictionaryIdAsDefault(@Param("dictionaryId") Integer dictionaryId, @Param("dictCode") String dictCode);
}
